package com.example.shop_2211.fragment

import android.annotation.SuppressLint
import android.app.Activity
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseFragment
import com.example.shop_2211.R
import com.example.shop_2211.databinding.FragmentMineBinding
import com.example.shop_2211.fgViewModel.MineViewModel
import com.example.shop_2211.utils.GlideUtils
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class MineFragment : BaseFragment<FragmentMineBinding,MineViewModel>() {
    override fun initView() {

        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.fragmentContainerView2) as NavHostFragment
        val navController = navHostFragment.navController
        binding.bnv.setupWithNavController(navController)

        viewModel.getUser()

        binding.userAddress.setOnClickListener {
            val addressPicker = AddressPicker(requireActivity())
            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            addressPicker.setOnAddressPickedListener { province, city, county ->
                binding.userAddress.text = "${province.name}${city.name}${county.name}"
            }
            addressPicker.show()
        }
        binding.userNickname.setOnClickListener {
            val view = LayoutInflater.from(context).inflate(R.layout.dialog_name, null)
            AlertDialog.Builder(requireActivity())
                .setTitle("修改昵称")
                .setView(view)
                .setPositiveButton("确定",object :DialogInterface.OnClickListener{
                    @SuppressLint("MissingInflatedId")
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        binding.userNickname.text = view.findViewById<EditText>(R.id.et).text.toString()
                    }
                })
                .setNegativeButton("取消",object :DialogInterface.OnClickListener{
                    override fun onClick(dialog: DialogInterface?, which: Int) {

                    }
                })
                .show()
        }
        binding.userImage.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
        //退出登录
        binding.logout.setOnClickListener {
            SPUtils.getInstance().clear()
            requireActivity().finish()
            ARouter.getInstance().build("/app/LoginCodeActivity").navigation()
        }
    }

    override fun initData() {
        viewModel.getUserSuccess.observe(this){
            context?.let { it1 -> GlideUtils.loadCircleImage(it1,it.data.avatar,binding.userImage) }
            binding.userNickname.text = it.data.nickname
        }
        viewModel.imageSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
    }

    override fun getViewModel(): Class<MineViewModel>  = MineViewModel::class.java

    override fun getLayout(): Int  = R.layout.fragment_mine

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==200&&resultCode==Activity.RESULT_OK){
            val pictureBean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideUtils.loadCircleImage(requireActivity(),pictureBean.path,binding.userImage)
            val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), File(pictureBean.path))
            val part = MultipartBody.Part.createFormData("multipart","aa.jpg",body)
            viewModel.image("user","0",part)
        }
    }

}